// pages/classify/classify.js
import Tips from '../../class/methods/Tips.js'
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    bar: [],
    list: [],
    currentTab: 0,
    id: 0, //选中bar的id默认第一个
    show: false,
    piontshow: '',
    uid: '',
    level: '',
    notice: '',
    page: 1
  },
  // 跳详情
  jumpdetail: function(e) {
    let id = e.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?id=' + id,
    })
  },
  // 跳系统消息
  jumpmessage() {
    wx.navigateTo({
      url: '/pages/message/message',
    })
  },
  // 跳搜索
  jumpsearch() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  // 选择分类
  swicthbar: function(e) {
    var that = this;
    console.log(e)
    let currentTab = e.currentTarget.dataset.idx; //左边id 0\1\2
    that.setData({
      currentTab: currentTab,
      id: e.currentTarget.dataset.id,
      page: 1
    })
    that.loadlist()
  },

  //请求列表
  loadlist() {
    var that = this
   
    Tips.loading()
    wx.request({
      url: app.globalData.api + "getgoodstypelist",
      data: {
        uid: that.data.uid,
        // level: that.data.level,
        pid: that.data.id,
        page: that.data.page
      },
      success: function(res) {
        Tips.loaded();
        console.log(res)
        if (res.data.code == 1) {
          console.log('嘻嘻嘻嘻嘻')
          that.setData({
            show: true,
            list: []
          })

        } else if (res.data.list.length == 0) {
          wx.showToast({
            title: '已经到底了',
          })
        } else {
          let list= []
          that.setData({
            show: false,
            list: list.concat(res.data.list),
            page: ++that.data.page
          })
        }
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        that.setData({
          uid: res.data.id,
          level: res.data.level
        })
      },
    })
    wx.request({
      url: app.globalData.api + "getgoodstypes",
      success: function(res) {
        console.log(res.data.list)
        that.setData({
          bar: res.data.list,
          id: res.data.list[0].id
        })
        that.loadlist()
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        //小红点状态
        wx.request({
          url: app.globalData.api + "getnoticemsg",
          data: {
            uid: res.data.id,
          },
          success: function(res) {
            console.log('小点点', res)
            if (res.data.code == 0) {
              that.setData({
                piontshow: true
              })
            } else {
              that.setData({
                piontshow: false
              })
            }
          },
        })

      },
      fail: function(res) {
        wx.showModal({
          title: '提示',
          content: '需授权后才购买',
          confirmColor: '#5FA15F',
          // showCancel: false,
          confirmText: '前往授权',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/login/welcome',
              })
            } else if (res.cancel) {

            }


          }
        })
      }
    })






  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.loadlist()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})